package com.xhcl.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xhcl.common.core.text.Convert;
import com.xhcl.common.exception.BusinessException;
import com.xhcl.system.domain.AccountReport;
import com.xhcl.system.domain.TSrhsHsxtcs;
import com.xhcl.system.domain.TSrhsKmye;
import com.xhcl.system.mapper.TSrhsKmyeMapper;
import com.xhcl.system.mapper.TSrhsPztoMapper;
import com.xhcl.system.service.ITSrhsHsxtcsService;
import com.xhcl.system.service.ITSrhsKmyeService;

/**
 * 科目余额Service业务层处理
 * 
 * @author xhcl
 * @date 2020-09-03
 */
@Service
public class TSrhsKmyeServiceImpl implements ITSrhsKmyeService 
{
    @Autowired
    private TSrhsKmyeMapper tSrhsKmyeMapper;
    

    @Autowired
    private TSrhsPztoMapper tSrhsPztoMapper;
    
    @Autowired
    private ITSrhsHsxtcsService tSrhsHsxtcsService;

    /**
     * 查询科目余额
     * 
     * @param nd 科目余额ID
     * @return 科目余额
     */
    @Override
    public TSrhsKmye selectTSrhsKmyeById(Long nd)
    {
        return tSrhsKmyeMapper.selectTSrhsKmyeById(nd);
    }

    /**
     * 查询科目余额列表
     * 
     * @param tSrhsKmye 科目余额
     * @return 科目余额
     */
    @Override
    public List<TSrhsKmye> selectTSrhsKmyeList(TSrhsKmye tSrhsKmye)
    {
        return tSrhsKmyeMapper.selectTSrhsKmyeList(tSrhsKmye);
    }

    /**
     * 新增科目余额
     * 
     * @param tSrhsKmye 科目余额
     * @return 结果
     */
    @Override
    public int insertTSrhsKmye(TSrhsKmye tSrhsKmye)
    {
        return tSrhsKmyeMapper.insertTSrhsKmye(tSrhsKmye);
    }

    /**
     * 修改科目余额
     * 
     * @param tSrhsKmye 科目余额
     * @return 结果
     */
    @Override
    public int updateTSrhsKmye(TSrhsKmye tSrhsKmye)
    {
        return tSrhsKmyeMapper.updateTSrhsKmye(tSrhsKmye);
    }

    /**
     * 删除科目余额对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTSrhsKmyeByIds(String ids)
    {
        return tSrhsKmyeMapper.deleteTSrhsKmyeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除科目余额信息
     * 
     * @param nd 科目余额ID
     * @return 结果
     */
    @Override
    public int deleteTSrhsKmyeById(Long nd)
    {
        return tSrhsKmyeMapper.deleteTSrhsKmyeById(nd);
    }
    public List<TSrhsKmye> selectTSrhsKmyeForNzjzList(){
    	return tSrhsKmyeMapper.selectTSrhsKmyeForNzjzList();
    }
    @Transactional
    public void doNzzz(List<TSrhsKmye> list,String userName) throws BusinessException{
    	TSrhsHsxtcs tSrhsHsxtcs=new TSrhsHsxtcs();
    	tSrhsHsxtcs.setCsdm("year");
    	List<TSrhsHsxtcs> ylist=tSrhsHsxtcsService.selectTSrhsHsxtcsList(tSrhsHsxtcs);
    	TSrhsHsxtcs tSrhsHsxtcs_term=new TSrhsHsxtcs();
    	String year=ylist.stream().findFirst().orElse(new TSrhsHsxtcs()).getCsnr();
    	tSrhsHsxtcs_term.setCsdm("term");
    	List<TSrhsHsxtcs> tlist=tSrhsHsxtcsService.selectTSrhsHsxtcsList(tSrhsHsxtcs_term);
    	String term=tlist.stream().findFirst().orElse(new TSrhsHsxtcs()).getCsnr();
    	if (!term.equals("12")) {
    		throw new BusinessException("年终转账只在12月才可处理");
    	}
    	if (list==null ||list.size()==0){
    		throw new BusinessException("结转的明细信息不存在");
    	}
    	int intCount=tSrhsPztoMapper.selectTSrhsPztoNoJzCount();
    	if (intCount>0) {
    		throw new BusinessException("存在未记账的凭证");
    	}
    	Map<String,Object> map=new HashMap<String,Object>();
    	map.put("nd", Integer.parseInt(year));
    	map.put("yf", Integer.parseInt(term));
    	tSrhsKmyeMapper.callBalanceCheck(map);
    	if (map.get("outrev")!=null && !map.get("outrev").equals("0")) {
    		throw new BusinessException(map.get("outrev").toString());
    	}
    	tSrhsKmyeMapper.insertTsrhsNzzz(list);
    	Map<String,Object> nzjzmap=new HashMap<String,Object>();
    	nzjzmap.put("clsj", new Date());
    	nzjzmap.put("zdr",  userName);
    	tSrhsKmyeMapper.callNzjz(nzjzmap);
    	if (nzjzmap.get("outrev")!=null && !nzjzmap.get("outrev").equals("ok")) {
    		throw new BusinessException(map.get("outrev").toString());
    	}
    }
    @Transactional
    public void doFinal	() throws BusinessException{
    	TSrhsHsxtcs tSrhsHsxtcs=new TSrhsHsxtcs();
    	tSrhsHsxtcs.setCsdm("year");
    	List<TSrhsHsxtcs> ylist=tSrhsHsxtcsService.selectTSrhsHsxtcsList(tSrhsHsxtcs);
    	TSrhsHsxtcs tSrhsHsxtcs_term=new TSrhsHsxtcs();
    	String year=ylist.stream().findFirst().orElse(new TSrhsHsxtcs()).getCsnr();
    	tSrhsHsxtcs_term.setCsdm("term");
    	List<TSrhsHsxtcs> tlist=tSrhsHsxtcsService.selectTSrhsHsxtcsList(tSrhsHsxtcs_term);
    	String term=tlist.stream().findFirst().orElse(new TSrhsHsxtcs()).getCsnr();
    	Map<String,Object> map=new HashMap<String,Object>();
    	map.put("nd", Integer.parseInt(year));
    	map.put("yf", Integer.parseInt(term));
    	tSrhsKmyeMapper.callBreakCheck(map);
    	if (map.get("outrev")!=null && !map.get("outrev").equals("ok")) {
    		throw new BusinessException(map.get("outrev").toString());
    	}
    	Map<String,Object> fmap=new HashMap<String,Object>();
    	fmap.put("nd", Integer.parseInt(year));
    	fmap.put("yf", Integer.parseInt(term));
    	tSrhsKmyeMapper.callFinal(map);
    	if (fmap.get("outrev")!=null && !fmap.get("outrev").equals("ok")) {
    		throw new BusinessException(fmap.get("outrev").toString());
    	}
    }
    @Transactional
    public void undoFinal () throws BusinessException{
    	Map<String,Object> map=new HashMap<String,Object>();
    	tSrhsKmyeMapper.callUnfinal(map);
    	if (map.get("outrev")!=null && !map.get("outrev").equals("ok")) {
    		throw new BusinessException(map.get("outrev").toString());
    	}
    }
    
    /**
     * 试算平衡
     * @param tSrhsKmye
     * @return
     */
    public List<AccountReport> selectTrialBalanceList(TSrhsKmye tSrhsKmye){
    	List<TSrhsKmye> list=tSrhsKmyeMapper.selectTrialBalanceList(tSrhsKmye);
    	return toReportList(list);
    }
    
    
    public List<AccountReport> toReportList(List<TSrhsKmye> list)
    {
      Iterator it = list.iterator();
      TSrhsKmye o = new TSrhsKmye();
      AccountReport total = new AccountReport();
      total.setKmdm("");
      total.setKmmc("合计:");
      while (it.hasNext()) {
        o = (TSrhsKmye)it.next();
        if (o.getJdfx() == 1) {
          o.setBeginLenderMoney(o.getQcye());
          o.setEndLenderMoney(
            o.getQcye().add(o.getJffse()).subtract(
            o.getDffse()));
        } else {
          o.setBeginBebitMoney(o.getQcye());
          o.setEndBebitMoney(
            o.getQcye().add(o.getDffse()).subtract(o.getJffse()));
        }
        if (o.getKmjb() == 1) {
          total.setBeginLenderMoney(total.getBeginLenderMoney()
            .add(o.getBeginLenderMoney()));
          total.setBeginBebitMoney(total.getBeginBebitMoney()
            .add(o.getBeginBebitMoney()));
          total.setNowLenderMoney(total.getNowLenderMoney()
            .add(o.getJffse()));
          total.setNowBebitMoney(total.getNowBebitMoney()
            .add(o.getDffse()));
          total.setEndLenderMoney(total.getEndLenderMoney()
            .add(o.getEndLenderMoney()));
          total.setEndBebitMoney(total.getEndBebitMoney()
            .add(o.getEndBebitMoney()));
        }
      }
      List<AccountReport> newList=new ArrayList<AccountReport>();
      for (TSrhsKmye kmye: list){
    	  AccountReport ac = new AccountReport(); 
    	  ac.setKmdm(kmye.getKmdm());
    	  ac.setKmmc(kmye.getKmmc());
    	  ac.setKmjb(kmye.getKmjb());
    	  ac.setKmjg(kmye.getKmjg());
    	  ac.setJdfx(kmye.getJdfx());
    	  ac.setBeginMoney(kmye.getQcye());
    	  ac.setBeginBebitMoney(kmye.getBeginBebitMoney());
    	  ac.setBeginLenderMoney(kmye.getBeginLenderMoney());
    	  ac.setNowBebitMoney(kmye.getDffse());
    	  ac.setNowLenderMoney(kmye.getJffse());
    	  ac.setEndBebitMoney(kmye.getEndBebitMoney());
    	  ac.setEndLenderMoney(kmye.getEndLenderMoney());
    	  newList.add(ac);
      }
      newList.add(total);
      return newList;
    }
    /**
     * 检查平衡
     * @param tSrhsKmye
     * @throws BusinessException
     */
    public void docallTrialBalanceCheck(TSrhsKmye tSrhsKmye) throws BusinessException{
    	Map<String,Object> map=new HashMap<String,Object>();
    	map.put("nd", tSrhsKmye.getNd());
    	map.put("yf", tSrhsKmye.getYf());
    	tSrhsKmyeMapper.callTrialBalanceCheck(map);
    	if (map.get("outrev")!=null && !map.get("outrev").equals("ok")) {
    		throw new BusinessException(map.get("outrev").toString());
    	}
    }
    public List<AccountReport> selectAccountSubjectList(TSrhsKmye tSrhsKmye){
    	List<TSrhsKmye> list=null;
    	if (tSrhsKmye.getParams().get("sfjz")!=null && tSrhsKmye.getParams().get("sfjz").equals("on")) {
    		list=tSrhsKmyeMapper.selectWjzAccountSubjectList(tSrhsKmye);
    	}else {
    		list=tSrhsKmyeMapper.selectJzAccountSubjectList(tSrhsKmye);
    	}
    	return toReportList(list);
    }
}
